{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 22\n",
    "\n",
    "# 向量夹角\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc1dff6-2af1-4b1d-849d-d4f95e7f1688",
   "metadata": {},
   "source": [
    "这段代码通过向量的点积公式来计算两个向量 $ \\vec{a} = [4, 1] $ 和 $ \\vec{b} = [1, 3] $ 之间的夹角。首先，代码计算了向量 $ \\vec{a} $ 和 $ \\vec{b} $ 的 $L2$ 范数，分别表示为 $||\\vec{a}||$ 和 $||\\vec{b}||$，即：\n",
    "\n",
    "$$\n",
    "||\\vec{a}|| = \\sqrt{4^2 + 1^2} = \\sqrt{17}\n",
    "$$\n",
    "\n",
    "$$\n",
    "||\\vec{b}|| = \\sqrt{1^2 + 3^2} = \\sqrt{10}\n",
    "$$\n",
    "\n",
    "接着，计算两向量的点积 $ \\vec{a} \\cdot \\vec{b} $：\n",
    "\n",
    "$$\n",
    "\\vec{a} \\cdot \\vec{b} = 4 \\cdot 1 + 1 \\cdot 3 = 4 + 3 = 7\n",
    "$$\n",
    "\n",
    "根据点积的定义，余弦值可以表示为：\n",
    "\n",
    "$$\n",
    "\\cos \\theta = \\frac{\\vec{a} \\cdot \\vec{b}}{||\\vec{a}|| \\cdot ||\\vec{b}||}\n",
    "$$\n",
    "\n",
    "代入已知数值得到：\n",
    "\n",
    "$$\n",
    "\\cos \\theta = \\frac{7}{\\sqrt{17} \\cdot \\sqrt{10}}\n",
    "$$\n",
    "\n",
    "最后，代码通过反余弦函数计算出夹角 $\\theta$ 的弧度值，并将其转换为角度。结果就是两个向量之间的角度，用度数表示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "247d6d2b-312e-4d1e-a86f-0b3a849c98e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95e598b2-6a7a-44f2-8638-ad223e81528e",
   "metadata": {},
   "source": [
    "## 定义向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9793e936-6ebd-4ce9-a355-f3aea835f56d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [4, 1]  # 向量 a\n",
    "b = [1, 3]  # 向量 b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc64fb3a-43d9-4bd9-99ba-f2ca3180e0eb",
   "metadata": {},
   "source": [
    "## 计算 L2 范数（向量长度）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1019e4e4-352f-4af1-8b01-28ac3b9d0eaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_norm = np.linalg.norm(a)  # 计算向量 a 的 L2 范数\n",
    "b_norm = np.linalg.norm(b)  # 计算向量 b 的 L2 范数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f86aff56-6d9e-412c-86ef-1ea269882af3",
   "metadata": {},
   "source": [
    "## 计算点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3adf24b8-a250-4cc5-8f33-346ab7a50a3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_dot_b = np.dot(a, b)  # 计算向量 a 和向量 b 的点积"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fadfc35-8183-430e-a628-890bd64b9dc2",
   "metadata": {},
   "source": [
    "## 计算余弦值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1cf2dbd2-8b96-43b2-b4dd-503167cd39fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "cos_result = a_dot_b / a_norm / b_norm  # 计算余弦值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d93e3ac-046d-4a21-8cec-8205759a234c",
   "metadata": {},
   "source": [
    "## 弧度转角度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "748a379a-c3ff-4ffd-868c-6390362cd758",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "57.52880770915151\n"
     ]
    }
   ],
   "source": [
    "angle = np.degrees(np.arccos(cos_result))  # 将弧度转换为角度\n",
    "print(angle)  # 打印角度结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
